package com.baide.sanguanheyi.sqlite;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @ClassName ContentServiceImpl
 * @Description TODO
 * @Author wangbing
 * @Date 2020/5/19 10:21
 **/
@Service
public class ContentServiceImpl implements ContentService {

    @Autowired
    private ContentDao dao;

    @Transactional(propagation= Propagation.SUPPORTS)
    @Override
    public Content save(Content content) {
        return dao.save(content);
    }

    @Override
    public Content findById(String barCode) {
        return dao.findById(barCode).orElse(null);
    }

    @Override
    public List<Content> findAll(Content content) {
        ExampleMatcher matcher = ExampleMatcher.matching()
                .withMatcher("voyageNo", ExampleMatcher.GenericPropertyMatchers.contains());
//        dao.findAll(Example.of())
        return null;
    }

    @Override
    public List<Content> findByTimeGreaterThanEqualAndTimeLessThanEqualAndVoyageNoContainingAndCustOrderContaining(Long starTIme, Long endTime, String voyageNo, String custOrder) {
        return dao.findByTimeGreaterThanEqualAndTimeLessThanEqualAndVoyageNoContainingAndCustOrderContaining(starTIme, endTime, voyageNo, custOrder);
    }

    @Override
    public Content save(String barCode, String weight) {
        Content content = dao.findById(barCode).orElse(null);
        if (content != null) {
            content.setWeight(weight);
            return dao.save(content);
        }
        return null;
    }

    @Transactional
    @Override
    public void deleteByTimeLessThan() {
//        dao.deleteByTimeLessThan(System.currentTimeMillis()- 24 * 7 * 60 * 60 * 1000);
    }

    @Override
    public List<Content> findByTimeGreaterThanEqualAndTimeLessThanEqualAndVoyageNoContainingAndCustOrderContainingAndCtOrderIs(Long starTIme, Long endTime, String voyageNo, String custOrder, String ctOrder) {
        return dao.findByTimeGreaterThanEqualAndTimeLessThanEqualAndVoyageNoContainingAndCustOrderContainingAndCtOrderIs(starTIme, endTime, voyageNo, custOrder, ctOrder);
    }

}
